<?xml version="1.0" encoding="UTF-8"?>
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:p="http://primefaces.org/ui"
	template="/WEB-INF/templates/default.xhtml">
	<ui:define name="content">
		<!-- Formulário de Pesquisa -->
		<h:form id="frmPesquisa" prependId="false" styleClass="form-horizontal">
			<p:outputPanel id="panelPesquisa" rendered="#{!usuarioController.mostraCadastro}">
				<fieldset>
					<legend>#{messages.TITLE_PESQUISA_USUARIO}</legend>
	
					<!-- Campos de Pesquisa -->
					<div class="control-group">
						<h:outputLabel for="txtNomeSearch" value="#{messages.LABEL_NOME}:" styleClass="control-label"/>
	                    <div class="controls">
							<p:inputText value="#{usuarioController.usuarioSearch.nome}" id="txtNomeSearch" maxlength="80" styleClass="input-xxlarge"/>
	                    </div>
					</div>

					<div class="control-group">
						<h:outputLabel for="txtLoginSearch" value="#{messages.LABEL_LOGIN}:" styleClass="control-label"/>
	                    <div class="controls">
							<p:inputText value="#{usuarioController.usuarioSearch.login}" id="txtLoginSearch" styleClass="input-xxlarge"/>
	                    </div>
					</div>

					<div class="control-group">
						<h:outputLabel for="cmbPerfilSearch" value="#{messages.LABEL_PERFIL}:" styleClass="control-label"/>
	                    <div class="controls">
							<h:selectOneMenu id="cmbPerfilSearch" value="#{usuarioController.usuarioSearch.perfil}" label="#{messages.LABEL_PERFIL}" converter="entityConverter">
								<f:selectItem itemLabel="Selecione o #{messages.LABEL_PERFIL}"/>
								<f:selectItems value="#{usuarioController.perfis}" var="perfil" itemLabel="#{perfil.nome}"/>
							</h:selectOneMenu>
						</div>
					</div>
					<!--End Campos de Pesquisa -->
	
					<!-- Primeiro Grupo de botões -->
					<div class="form-actions">
						<p:commandButton id="btnPesquisar" value="#{messages.LABEL_PESQUISAR}" action="#{usuarioController.pesquisar()}"
							process="frmPesquisa" update="frmPesquisa" styleClass="btn btn-primary"/>

						<p:commandButton id="btnLimpar" value="#{messages.LABEL_LIMPAR}" action="#{usuarioController.limpar()}"
							process="@this" update="frmPesquisa" styleClass="btn btn-primary"/>
	
						<p:commandButton id="btnNovo" value="#{messages.LABEL_NOVO}" action="#{usuarioController.novo()}" 
	                    	process="@this" update="frmPesquisa, :frmCadastro" styleClass="btn btn-primary"/>
	                </div>
	                <!--End Primeiro Grupo de botões -->
				</fieldset>
	
				<!-- Grid de dados -->
				<p:dataTable id="tblUsuario" var="usuario" value="#{usuarioController.usuarios}" rows="10" paginator="true" 
	           		style="#{empty usuarioController.usuarios ? 'display:none;' : 'display:table; width: 100%;'}" styleClass="table table-bordered">

	             	<f:facet name="header">
	             		<h:outputText value="#{messages.TITLE_PESQUISAR}"/>
	     			</f:facet>

	     		    <p:column headerText="#{messages.LABEL_NOME}" >  
	                    <h:outputText value="#{usuario.nome}"/>
	                </p:column>  

	                <p:column headerText="#{messages.LABEL_LOGIN}">
	                	 <h:outputText value="#{usuario.login}"/>
	                </p:column>  

	                <p:column headerText="#{messages.LABEL_PERFIL}">
	                	 <h:outputText value="#{usuario.perfil.nome}"/> 
	                </p:column>
	                
	                <p:column headerText="#{messages.LABEL_EMAIL}">
	                	 <h:outputText value="#{usuario.email}"/> 
	                </p:column>

					<!-- Segundo Grupo de botões -->
	                <p:column headerText="#{messages.LABEL_ACOES}" style="text-align: center !important;">
	               		<p:commandLink id="btnAlterar" title="#{messages.LABEL_ALTERAR}" action="#{usuarioController.alterar(usuario)}"
	                		update=":frmPesquisa, :frmCadastro">
	                		<p:graphicImage value="#{resource['images/edit.png']}" />
	                	</p:commandLink>

	                	<p:spacer height="0" width="20"/>

	                	<p:commandLink id="btnRemover" title="#{messages.LABEL_EXCLUIR}" onclick="confirmModal.show();">
	                		<f:setPropertyActionListener value="#{usuario}" target="#{usuarioController.usuario}"/>
	                		<p:graphicImage value="#{resource['images/trash.png']}" />
	                	</p:commandLink>
	                </p:column>
	                <!-- End Segundo Grupo de botões -->
				</p:dataTable>
				<!-- End Grid de dados -->
			</p:outputPanel>
		</h:form>
		<!-- End Formulário de Pesquisa -->

		<!-- Formulário de Cadastro -->
		<h:form id="frmCadastro" prependId="false" styleClass="form-horizontal">
			<p:outputPanel id="panelCadastro" rendered="#{usuarioController.mostraCadastro}">
				<legend style="#{usuarioController.isCadastro ? '' : 'display: none;'}">#{messages.TITLE_CADASTRO_USUARIO}</legend>
				<legend style="#{!usuarioController.isCadastro ? '' : 'display: none;'}">#{messages.TITLE_ALTERACAO_USUARIO}</legend>

				<div class="control-group">
					<h:outputLabel for="txtCpf" value="#{messages.LABEL_CPF}:" styleClass="control-label required"/>
                    <div class="controls">
						<p:inputMask id="txtCpf" value="#{usuarioController.usuario.cpf}" maxlength="11"
							label="#{messages.LABEL_CPF}" styleClass="input-xxlarge cpf" mask="999.999.999-99" converter="cpfConverter" validator="cpfValidator">
							<p:ajax event="blur" update="txtNome" listener="#{usuarioController.changeCpf}"/>
						</p:inputMask>
					</div>
				</div>

				<div class="control-group">
					<h:outputLabel for="txtNome" value="#{messages.LABEL_NOME}:" styleClass="control-label required"/>
                    <div class="controls">
						<p:inputText id="txtNome" value="#{usuarioController.usuario.nome}" maxlength="80"
							label="#{messages.LABEL_NOME}" styleClass="input-xxlarge" disabled="true"/>
					</div>
				</div>

				<div class="control-group">
					<h:outputLabel for="txtLogin" value="#{messages.LABEL_LOGIN}:" styleClass="control-label required"/>
                    <div class="controls">
						<p:inputText id="txtLogin" value="#{usuarioController.usuario.login}" maxlength="40"
							label="#{messages.LABEL_LOGIN}" styleClass="input-xxlarge"/>
					</div>
				</div>

				<div class="control-group">
					<h:outputLabel for="txtSenha" value="#{messages.LABEL_SENHA}:" styleClass="control-label required"/>
                    <div class="controls">
						<h:inputSecret id="txtSenha" value="#{usuarioController.usuario.senha}" maxlength="100"
							label="#{messages.LABEL_SENHA}" styleClass="input-xxlarge"/>
					</div>
				</div>

				<div class="control-group">
					<h:outputLabel for="cmbPerfil" value="#{messages.LABEL_PERFIL}:" styleClass="control-label required"/>
                    <div class="controls">
						<h:selectOneMenu id="cmbPerfil" value="#{usuarioController.usuario.perfil}" label="#{messages.LABEL_PERFIL}" converter="entityConverter">
							<f:selectItem itemLabel="Selecione o #{messages.LABEL_PERFIL}"/>
							<f:selectItems value="#{usuarioController.perfis}" var="perfil" itemLabel="#{perfil.nome}"/>
						</h:selectOneMenu>
					</div>
				</div>

				<div class="control-group">
					<h:outputLabel for="txtEmail" value="#{messages.LABEL_EMAIL}:" styleClass="control-label required"/>
                    <div class="controls">
						<p:inputText id="txtEmail" value="#{usuarioController.usuario.email}" maxlength="50"
							label="#{messages.LABEL_EMAIL}" styleClass="input-xxlarge" validator="emailValidator">
							<p:ajax event="blur" update="msgEmail"/>
						</p:inputText>
						<p:message id="msgEmail" for="txtEmail"/>
					</div>
				</div>

				<div class="form-actions">
					<p:commandButton id="btnSalvar" value="#{messages.BTN_SALVAR}" action="#{usuarioController.salvar()}"
	                	update=":frmCadastro, :frmPesquisa" styleClass="btn" oncomplete="Util.scrollTop();" process="frmCadastro"/>
	                	
	                <p:commandButton value="#{messages.LABEL_CANCELAR}" action="#{usuarioController.cancelar()}" 
						update=":frmCadastro, :frmPesquisa" immediate="true" styleClass="btn" process="frmCadastro"/>
				</div>
			</p:outputPanel>
		</h:form>
		
		<!-- Dialog de confirmação de exclusão -->
       	<h:form prependId="false">
			<p:confirmDialog message="#{messages.TITLE_HEADER_REMOVER}" header="Confirmação" severity="alert" widgetVar="confirmModal">
				<p:commandButton value="#{messages.LABEL_SIM}" inline="true" process="@this" actionListener="#{usuarioController.remover()}"
					onclick="confirmModal.hide();" update=":frmPesquisa" oncomplete="Util.scrollTop();"/>

				<p:commandButton value="#{messages.LABEL_NAO}" type="button" onclick="confirmModal.hide();"/>
			</p:confirmDialog>
		</h:form>
       	<!-- End Dialog de confirmação de exclusão -->
	</ui:define>
</ui:composition>